// pages/bindWechatDelete/bindWechatDelete.js
const app = getApp();
import CommonService from '../../utils/service/commonService.js';
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    //发送验证码按钮文字
    sendBtnMsg:'发送验证码',
    //发送验证码按钮是否禁用
    disabledMsg: false,
    //正确的验证码
    rightCode:'',
    //错误提示
    smsMessage:'',
    //当前用户
    user:{},
    //验证码
    checkCode:null,
    //绑定的id
    bindId:null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.id){
      this.setData({
        bindId: parseInt(options.id)
      })
    }
    //登录用户
    this.setData({
      user: app.globalData.userInfo
    })
  },

  //点击发送验证码
  sendCode(){
    Toast.loading({
      duration: 0,
      forbidClick: true,
      //message: '...'
    });

    let phone = this.data.user.personalPhone;
    if(phone){
      let parameter={
        personalPhone: phone,
        appId:[app.globalData.myAppId]
      }

      CommonService.sendSms(parameter).then(res=>{
        if(res.data.data){
          //查询验证码
          this.querySms(phone,res.data.data.bizId);
        }else{
          Toast.fail('抱歉出错了');
        }
      }).catch((error) => {
        Toast.fail('抱歉出错了');
      })
    }else{
      Toast('请到管理后台设置您的手机号码');
    }
  },

  //查询验证码
  querySms(phone,bizId){
    let parameter={
      personalPhone: phone,
      bizId:bizId
    }

    CommonService.querySms(parameter).then(res=>{
      Toast.clear();
      if(res.data.data){
        Toast.success('验证码已发送');

        console.log('验证码');
        console.log(res.data.data.code);

        this.setData({
          checkCode: res.data.data.code
        })

        //重新发送倒计时
        let time = null;
        let num = 60;

        time = setInterval(() => {
          num--;
          this.setData({
            disabledMsg: true,
            sendBtnMsg: num + '秒后重新发送'
          });
          if (num == 0) {
            clearInterval(time);
            this.setData({
              disabledMsg: false,
              sendBtnMsg: '发送验证码'
            });
          }
        }, 1000);
      }else{
        Toast.fail('抱歉出错了');
      }

    }).catch((error) => {
      Toast.fail('抱歉出错了');
    })
  },

  //获取输入的验证码
  getCode(e) {
    this.setData({
      sms: e.detail,
      smsMessage:'',
      disabled: false
    });
  },

  //用户输入时清除错误提示
  clearMessage(e) {
    let field = e.target.dataset.message;
    let obj = {};
    obj[field] = "";
    obj['disabled'] = false;
    this.setData(obj);
  },

  //提交表单-验证短信验证码
  submitForm(e){
    let sms = e.detail.value.sms;
    if (sms) {
      if (sms == this.data.checkCode){
        //解除绑定
        this.deleteBind();
      }else{
        this.setData({
          smsMessage: '请输入正确的验证码',
          disabled: true
        });
      }
    }else{
      this.setData({
        smsMessage: '请输入验证码',
        disabled: true
      });
    }
  }, 
  
  //解除绑定
  deleteBind(){
    let param={
      className:"wechatLogin",
      data:{
        id:this.data.bindId
      }
    }

    CommonService.goDelete(param).then(res=>{
      if (res.statusCode == 200 && res.data.success == true){
        Toast.success('已解绑');
        setTimeout(() => {
          wx.redirectTo({
            url: '/pages/set/set'
          })
        }, 1000);
      }else{
        Toast.fail('操作失败');
      }
    }).catch((error) => {
      Toast.fail('操作失败');
    })
  },  

  //无验证码这一步骤的解除绑定
  goToBindDelete(){
    this.deleteBind();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  }
})
